## 构建阶段：安装依赖并构建产物
FROM node:20-alpine AS builder
WORKDIR /app

# 仅复制依赖清单，加速缓存
COPY package.json pnpm-lock.yaml* ./

# 使用 corepack 管理 pnpm，避免全局安装
RUN corepack enable pnpm \
  && pnpm --version \
  && pnpm install --frozen-lockfile --prod=false

# 复制源代码并构建
COPY . .
ENV NODE_ENV=production
RUN pnpm run build  && pnpm prune --prod

## 运行阶段：仅包含运行所需文件
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production

# 复制生产依赖与构建产物
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/.output ./.output

EXPOSE 3000
CMD ["node", ".output/server/index.mjs"]
